from TikTokApi import TikTokApi
import json
from datetime import datetime
import random
proxies = {
  "http": "http://scraperapi:7383d26700b0269bfe37eaadc1eab541@proxy-server.scraperapi.com:8001",
  "https": "http://scraperapi:7383d26700b0269bfe37eaadc1eab541@proxy-server.scraperapi.com:8001"
}
verifyfp='verify_dc7673cc6efeefd71228df70374029cc'
# api = TikTokApi.get_instance(custom_verifyFp=verifyfp)
proxy="http://100.24.216.83:80"
did = str(random.randint(10000, 999999999))
api = TikTokApi.get_instance(use_test_endpoints=True, proxy=proxy, custom_did=did)



import os
# i=0
# userlist=api.bySound('6820654145293273090',count=5000)
# with open('sound6820654145293273090_5000_new.json', 'w', encoding='utf-8',newline='\n') as filename_output:
#     for tt in userlist:
#         i+=1
#         print(i)
#         filename_output.write(json.dumps(tt)+'\n')

# tiktokData = api.get_Video_By_Url(
# #     "https://v16-web-newkey.tiktokcdn.com/bc1d826cb318146aeef927858a602e1b/5f8a5898/video/tos/useast2a/tos-useast2a-ve-0068c001/84577df0c6cd402b87cb3865a3ea85af/?a=1988&br=2916&bt=1458&cr=0&cs=0&cv=1&dr=0&ds=3&er=&l=2020101620355701019017601435096290&lr=tiktok_m&mime_type=video_mp4&qs=0&rc=M2V1NDt5bHBpdjMzNjczM0ApZjVpM2hoNDs0N2U5ZjdoOGdxYWQta2ZtbjFfLS0wMTZzc2EwLmEtY2A0MF8uNjJgLzY6Yw%3D%3D&vl=&vr="
# # )
# tiktokData=api.get_Video_By_DownloadURL(api.getTikTokById('6848002044318584069')['itemInfo']['itemStruct']["video"]["downloadAddr"])
# with open("sample1.mp4", "wb") as out:
#     out.write(tiktokData)
htlist=api.discoverHashtags(did=did,proxy=proxy,custom_verifyFp=verifyfp)
mlist=api.discoverMusic(did=did,proxy=proxy,custom_verifyFp=verifyfp)
ht=[ 'foodtiktok','#nonuancenovember','#gamingsetup','#carsoftiktok','#needtoknow','#whenwewereyounger','#halloweenishere','#ourtype','#fallguysmoments','#workingathome','#onlinedating','#familyrecipe','#givingthanks','#diceroll','#holidayvibes','#holidaytiktok','#recordsday','#inkdrawing','#happyholidays','#homecooked','#neonshadow',  '#youwantmore' ,'#familyimpression','#givingszn','#whereilive','foodtiktok','#holidaymusic','#howbizarre', '#personalfinance'
     ,'#thinkingabout', '#housetour','#readysetshop','#easydiy','#coldweather','#wildanimals','#festivefashion','graphicdesign','smallbusiness','#halloweenlook','fallfashion','#productivity','#bekind','#onhold','#growupwithme','#tabletop','#theatrekids','#selfimprovement', '#OhNo','#animation']
# ht=['#UnwrapTheDeals','#ShowUpShowOff','#MicellarRewind','#HeinzHalloween']'#rnbvibes', '#halloweenishere' '#ourtype' '57419989', ,'#halloweenlook','#youwantmore','#animation', '#youwantmore','#diwali', '#smallbusiness'  '#recordsday', '#rnbvibes', ,'#diwali'  ,'whereilive','graphicdesign', '#youwantmore'  '#familyimpression','#youwantmore',,'#foodtiktok' '#watchmegrow'  '#oneyourthere', '#RatatouilleMusical'
# ht=['#bakingszn','#halloweenishere','#RoomTour']
m=['57419989','6883993546467051522','6600546691055618818','6717499894333966337','6844610572080794373','6808287098886424577', '6812545347835922434', '6807966889273346822', '6810951570629330945', '6813134956269947654', '6827517892439968517', '6834110584288447237', '6754420279457565445', '6746993352891189249', '6691366574592396038','6883297755284949765','255240484879720448','6889276181086996481']
t='2020-12-24'
numht={}
numm={}
n=1
for item in htlist:
    key=item['cardItem']['title'].replace('#','')
    numht.update({key:n})
    n+=1
    if item['cardItem']['title'] not in ht:
        ht.append(item['cardItem']['title'])
        print('Add:',item['cardItem']['title'])
n=1
for item in mlist:
    numm.update({item['cardItem']['id']: n})
    n += 1
    if item['cardItem']['id'] not in m:
        m.append(item['cardItem']['id'])
        print('Add:', item['cardItem']['id'])
print(len(htlist),len(mlist))
print(ht)
print(m)
for item in htlist:
    print(item['cardItem']['title'].replace('#',''))
for hashtag in ht:
    h=hashtag.replace('#','')
    k=0
    if h in numht.keys():
        k=numht[h]
    if 'Hashtag_' + h + '_' + str(t) +'_'+str(k)+ '.json' not in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
        hashtags=api.byHashtag(h,did=did,custom_verifyFp=verifyfp,proxy=proxy,count=2000)

        with open('D:\\Work\\Tool\\tiktok\\TikToks\\Hashtag_'+h+'_'+str(t)+'_'+str(k)+'.json', 'w', encoding='utf-8', newline='\n') as filename_output:
            i=0
            for tt in hashtags:
                i+=1
                filename_output.write(json.dumps(tt)+'\n')
            print(h,i,k)

for music in m:
    k = 0
    if music in numm.keys():
        k = numm[music]
    if 'Sound_' + music + '_' + str(t) +'_'+str(k)+ '.json' not in os.listdir('D:\\Work\\Tool\\tiktok\\TikToks\\'):
        musics=api.bySound(music,did=did,proxy=proxy,count=10000,custom_verifyFp=verifyfp)
        with open('D:\\Work\\Tool\\tiktok\\TikToks\\Sound_'+music+'_'+str(t)+'_'+str(k)+'.json', 'w', encoding='utf-8', newline='\n') as filename_output:
            i=0
            for tt in musics:
                i+=1
                filename_output.write(json.dumps(tt)+'\n')
            print(music,i,k)

